package com.csi.dao.impl;

import com.csi.dao.OrderDao;
import com.csi.domain.Order;
import com.csi.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl extends JDBCUtils implements OrderDao {
    @Override
    public List<Order> findAll() throws SQLException {
//        查询所有的订单
        final String sql="select*from hotel_order";
//调用jdbc连接数据库
        Connection connection = this.getConnection();
//执行SQL语句
        PreparedStatement ps = connection.prepareStatement(sql);
//表明是进行查询
        ResultSet rs = ps.executeQuery();
//声明list集合用于存储订单
        List<Order> list=new ArrayList<>();

//循环存出到list集合中
        while (rs.next()){

            Order order=new Order();

            order.setOrderId(rs.getInt("orderId"));

            order.setOrderDate(rs.getDate("orderDate"));

            order.setOrderRevenue(rs.getDouble("orderRevenue"));

            order.setOrderState(rs.getString("orderState"));

            order.setEmployId(rs.getInt("employId"));

            order.setGuestsId(rs.getInt("guestsId"));

            order.setRoomId(rs.getInt("roomId"));

            order.setUserId(rs.getInt("userId"));

            order.setStayDate(rs.getDate("stayDate"));

            order.setLeaveDate(rs.getDate("leaveDate"));

            list.add(order);

        }
        this.release(rs,ps,connection);

        return list;
    }

    @Override
    public int delete(int orderId) throws SQLException {
        final String sql="delete from hotel_order where orderId=?";

        Connection connection = this.getConnection();

        PreparedStatement ps = connection.prepareStatement(sql);

        ps.setInt(1,orderId);

        int i = ps.executeUpdate();
        return i;
    }

    @Override
    public int insert(Order order) throws SQLException {
        final  String sql="insert into hotel_order(orderState,orderDate,orderRevenue" +
                ",stayDate,leaveDate,roomId,employId,guestsId,userId)values(?,?,?,?,?,?,?,?,?)";
        Connection connection = this.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,order.getOrderState());
        ps.setDate(2, (Date) order.getOrderDate());
        ps.setDouble(3,order.getOrderRevenue());
        ps.setDate(4, (Date) order.getStayDate());
        ps.setDate(5, (Date) order.getLeaveDate());
        ps.setInt(6,order.getRoomId());
        ps.setInt(7,order.getEmployId());
        ps.setInt(8,order.getGuestsId());
        ps.setInt(9,order.getUserId());

        int i = ps.executeUpdate();

        this.release(ps,connection);


        return i;
    }

    @Override
    public int update(Order order) throws SQLException {
        final  String sql="update hotel_order set orderState=?,orderDate=?,orderRevenue=?,stayDate=?,leaveDate=?,roomId=?,employId=?,guestsId=?,userId=? where orderId=?";
        Connection connection = this.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,order.getOrderState());
        ps.setDate(2, (Date) order.getOrderDate());
        ps.setDouble(3,order.getOrderRevenue());
        ps.setDate(4, (Date) order.getStayDate());
        ps.setDate(5, (Date) order.getLeaveDate());
        ps.setInt(6,order.getRoomId());
        ps.setInt(7,order.getEmployId());
        ps.setInt(8,order.getGuestsId());
        ps.setInt(9,order.getUserId());
        ps.setInt(10,order.getOrderId());

        int i = ps.executeUpdate();

        this.release(ps,connection);

        return i;
    }
}
